/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package webserver;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;

/**
 *
 * @author Lars
 */
public class Tester {

	/**
	 * @param args the command line arguments
	 */
	public static void main(String[] args) {
		try {
			Socket testSocket = new Socket("localhost", AppProperties.getInstance().getPort());
			ArrayList<String> requests = new ArrayList<String>();
			//Standard HTTP1.0 GET Request
			requests.add(
					"GET /test.htm HTTP/1.0\r\n\r\n");
			//Standard HTTP1.1 GET Request
			requests.add(
					"GET /test.htm HTTP/1.1\r\n" +
					"Host: localhost\r\n\r\n");
			//Standard HTTP1.1 HEAD Request
			requests.add(
					"HEAD test.htm HTTP/1.1\r\n" +
					"Host: localhost\r\n\r\n");
			//Standard HTTP1.0 HEAD Request
			requests.add(
					"HEAD test.htm HTTP/1.0\r\n\r\n");
			//Standard HTTP1.1 POST Request
			String postData = "name=blubber&passwort=blafasel";
			requests.add(
					"POST /test.htm HTTP/1.1\r\n" +
					"Host: localhost\r\n" +
					"Content-Length: " +
					postData.length() +
					"\r\n\r\n" +
					postData +
					"\r\n");
			//If-Unmodified-Since HTTP1.1 GET Request
			requests.add(
					"GET test.htm HTTP/1.1\r\n" +
					"Host: localhost\r\n" +
					"If-Unmodified-Since: Thu, 27 May 2010 07:25:00 GMT-00:00\r\n\r\n");
			//If-Modified-Since HTTP1.1 GET Request
			requests.add("GET test.htm HTTP/1.1\r\n" +
					"Host: localhost\r\n" +
					"If-Modified-Since: Thu, 27 May 2010 07:25:00 GMT-00:00\r\n\r\n");
			//If-Modified-Since HTTP1.1 HEAD Request
			requests.add("HEAD test.htm HTTP/1.1\r\n" +
					"Host: localhost\r\n" +
					"If-Modified-Since: Thu, 27 May 2010 07:25:00 GMT-00:00\r\n\r\n");
			//If-Modified-Since HTTP1.1 GET Request
			requests.add("GET test.htm HTTP/1.1\r\n" +
					"Host: localhost\r\n" +
					"If-Modified-Since: Thu, 27 May 2010 07:25:00 GMT-00:00\r\n" +
					"Expect: blablub\r\n\r\n");

			for (String request : requests) {
				System.out.println("#-#-#-# Request Start #-#-#-#");
				System.out.println("Request to send:");
				System.out.print(request);
				BufferedOutputStream bos = new BufferedOutputStream(testSocket.getOutputStream());
				BufferedInputStream bis = new BufferedInputStream(testSocket.getInputStream());
				bos.write(request.getBytes());
				bos.flush();
				boolean arrived = false;
				StringBuffer response = new StringBuffer();
				while (!arrived) {
					while (bis.available() > 0) {
						arrived = true;
						response.append(Character.toChars(bis.read()));
					}
					Thread.sleep(10);
				}
				System.out.println("---------------------------------------------");
				System.out.println(response.toString());
				if (response.toString().contains("Connection: close")) {
					testSocket.close();
				}
				System.out.println("#-#-#-# Request Ende #-#-#-#\n");
				if (testSocket.isClosed()) {
					testSocket = new Socket("localhost", AppProperties.getInstance().getPort());
					System.out.println("Connection reopened!!!\n");
				} else {
					System.out.println("Connection not reopened!!!\n");
				}
			}
			testSocket.close();
		} catch (InterruptedException ie) {
			ie.printStackTrace();
		} catch (UnknownHostException ex) {
			ex.printStackTrace();
			//Logger.getLogger(Tester.class.getName()).log(Level.SEVERE, null, ex);
		} catch (IOException ex) {
			ex.printStackTrace();
			//Logger.getLogger(Tester.class.getName()).log(Level.SEVERE, null, ex);
		}
	}
}
